﻿var userAgent = navigator.userAgent.toLowerCase();
var is_opera = userAgent.indexOf('opera') != -1 && opera.version();
var is_moz = (navigator.product == 'Gecko') && userAgent.substr(userAgent.indexOf('firefox') + 8, 3);
var is_ie = (userAgent.indexOf('msie') != -1 && !is_opera) && userAgent.substr(userAgent.indexOf('msie') + 5, 3);
var is_mac = userAgent.indexOf('mac') != -1;
var InFloat = '';

function isUndefined(variable) {
    return typeof variable == 'undefined' ? true : false;
}

function doane(event) {
    e = event ? event : window.event;
    if (is_ie) {
        e.returnValue = false;
        e.cancelBubble = true;
    } else if (e) {
        e.stopPropagation();
        e.preventDefault();
    }
}

function CheckByName(form, tname, checked) {
    for (var i = 0; i < form.elements.length; i++) {
        var e = form.elements[i];
        if (e.name == tname) {
            e.checked = checked;
        }
    }
}

function CheckAll(form) {
    for (var i = 0; i < form.elements.length; i++) {
        var e = form.elements[i];
        if (e.type == "checkbox" && e.name != 'chkall' && e.name != 'deleteMode') {
            e.checked = form.chkall.checked;
        }
    }
}

function CheckSpecial(form, obname, ckname) {
    for (var i = 0; i < form.elements.length; i++) {
        var e = form.elements[i];
        if (e.type == "checkbox" && e.name == ckname) {
            e.checked = obname.checked;
        }
    }
}

function checkedEnabledButton() {
    for (var i = 0; i < arguments[0].elements.length; i++) {
        var e = arguments[0].elements[i];
        if (e.name == arguments[1] && e.checked) {
            for (var j = 2; j < arguments.length; j++) {
                if (document.getElementById(arguments[j])) {
                    document.getElementById(arguments[j]).disabled = false;
                }
            }
            return;
        }
    }
    for (var j = 2; j < arguments.length; j++) {
        if (document.getElementById(arguments[j])) {
            document.getElementById(arguments[j]).disabled = true;
        }
    }
}


//隐藏指定元素位置下方的"下拉列表框",该函数主题解决ie6以前(包括ie6)的下拉列表框遮挡显示层的问题
function HideOverSels(objID) {
    var sels = document.getElementsByTagName('select');

    for (var i = 0; i < sels.length; i++) {
        if (Obj1OverObj2(document.getElementById(objID), sels[i])) {

            sels[i].style.visibility = 'hidden';
        }
        else {
            sels[i].style.visibility = 'visible';
        }
    }
}

function getLeftPosition(Obj) {
    try {
        for (var sumLeft = 0; Obj != document.body; sumLeft += Obj.offsetLeft, Obj = Obj.offsetParent);
        return sumLeft;
    }
    catch (e)
    { }
}

function getTopPosition(Obj) {
    try {
        for (var sumTop = 0; Obj != document.body; sumTop += Obj.offsetTop, Obj = Obj.offsetParent);
        return sumTop;
    }
    catch (e)
    { }
}

//判断obj1是否遮挡了obj2
function Obj1OverObj2(obj1, obj2) {
    var result = true;

    var obj1Left = getLeftPosition(obj1) - document.body.scrollLeft;
    var obj1Top = getTopPosition(obj1) - document.body.scrollTop;
    var obj1Right = obj1Left + obj1.offsetWidth;
    var obj1Bottom = obj1Top + obj1.offsetHeight;
    var obj2Left = getLeftPosition(obj2) - document.body.scrollLeft;
    var obj2Top = getTopPosition(obj2) - document.body.scrollTop;
    var obj2Right = obj2Left + obj2.offsetWidth;
    var obj2Bottom = obj2Top + obj2.offsetHeight;


    if (obj1Right <= obj2Left || obj1Bottom <= obj2Top || obj1Left >= obj2Right || obj1Top >= obj2Bottom) {
        result = false;
    }

    return result;
}

////////////////////////////
//显示menu操作
////////////////////////////
var jsmenu = new Array();
var ctrlobjclassName;
jsmenu['active'] = new Array();
jsmenu['timer'] = new Array();
jsmenu['iframe'] = new Array();


var JSMENU = [];
JSMENU['active'] = [];
JSMENU['timer'] = [];
JSMENU['drag'] = [];
JSMENU['layer'] = 0;
JSMENU['zIndex'] = { 'win': 200, 'menu': 300, 'prompt': 400, 'dialog': 500 };
JSMENU['float'] = '';

function initCtrl(ctrlobj, click, duration, timeout, layer) {
    if (ctrlobj && !ctrlobj.initialized) {
        ctrlobj.initialized = true;
        ctrlobj.unselectable = true;

        ctrlobj.outfunc = typeof ctrlobj.onmouseout == 'function' ? ctrlobj.onmouseout : null;
        ctrlobj.onmouseout = function () {
            if (this.outfunc) this.outfunc();
            if (duration < 3 && !jsmenu['timer'][ctrlobj.id]) jsmenu['timer'][ctrlobj.id] = setTimeout('hideMenu(' + layer + ')', timeout);
        }

        ctrlobj.overfunc = typeof ctrlobj.onmouseover == 'function' ? ctrlobj.onmouseover : null;
        ctrlobj.onmouseover = function (e) {
            doane(e);
            if (this.overfunc) this.overfunc();
            if (click) {
                clearTimeout(jsmenu['timer'][this.id]);
                jsmenu['timer'][this.id] = null;
            } else {
                for (var id in jsmenu['timer']) {
                    if (jsmenu['timer'][id]) {
                        clearTimeout(jsmenu['timer'][id]);
                        jsmenu['timer'][id] = null;
                    }
                }
            }
        }
    }
}

function initMenu(ctrlid, menuobj, duration, timeout, layer, drag) {
    if (menuobj && !menuobj.initialized) {
        menuobj.initialized = true;
        menuobj.ctrlkey = ctrlid;
        menuobj.onclick = ebygum;
        menuobj.style.position = 'absolute';
        if (duration < 3) {
            if (duration > 1) {
                menuobj.onmouseover = function () {
                    clearTimeout(jsmenu['timer'][ctrlid]);
                    jsmenu['timer'][ctrlid] = null;
                }
            }
            if (duration != 1) {
                menuobj.onmouseout = function () {
                    jsmenu['timer'][ctrlid] = setTimeout('hideMenu(' + layer + ')', timeout);
                }
            }
        }
        menuobj.style.zIndex = 999 + layer;
        if (ctrlid.indexOf("calendarexp") != -1)
            menuobj.style.zIndex = 10003;
        if (drag) {
            menuobj.onmousedown = function (event) { try { menudrag(menuobj, event, 1); } catch (e) { } };
            menuobj.onmousemove = function (event) { try { menudrag(menuobj, event, 2); } catch (e) { } };
            menuobj.onmouseup = function (event) { try { menudrag(menuobj, event, 3); } catch (e) { } };
        }
    }
}

var menudragstart = new Array();
function menudrag(menuobj, e, op) {
    if (op == 1) {
        if (in_array(is_ie ? event.srcElement.tagName : e.target.tagName, ['TEXTAREA', 'INPUT', 'BUTTON', 'SELECT'])) {
            return;
        }
        menudragstart = is_ie ? [event.clientX, event.clientY] : [e.clientX, e.clientY];
        menudragstart[2] = parseInt(menuobj.style.left);
        menudragstart[3] = parseInt(menuobj.style.top);
        doane(e);
    } else if (op == 2 && menudragstart[0]) {
        var menudragnow = is_ie ? [event.clientX, event.clientY] : [e.clientX, e.clientY];
        menuobj.style.left = (menudragstart[2] + menudragnow[0] - menudragstart[0]) + 'px';
        menuobj.style.top = (menudragstart[3] + menudragnow[1] - menudragstart[1]) + 'px';
        doane(e);
    } else if (op == 3) {
        menudragstart = [];
        doane(e);
    }
}

function showMenu(ctrlid, click, offset, duration, timeout, layer, showid, maxh, drag) {
    var ctrlobj = document.getElementById(ctrlid);
    if (!ctrlobj) return;
    if (isUndefined(click)) click = false;
    if (isUndefined(offset)) offset = 0;
    if (isUndefined(duration)) duration = 2;
    if (isUndefined(timeout)) timeout = 250;
    if (isUndefined(layer)) layer = 0;
    if (isUndefined(showid)) showid = ctrlid;
    var showobj = document.getElementById(showid);
    var menuobj = document.getElementById(showid + '_menu');
    if (!showobj || !menuobj) return;
    if (isUndefined(maxh)) maxh = 400;
    if (isUndefined(drag)) drag = false;

    if (click && jsmenu['active'][layer] == menuobj) {
        hideMenu(layer);
        return;
    } else {
        hideMenu(layer);
    }

    var len = jsmenu['timer'].length;
    if (len > 0) {
        for (var i = 0; i < len; i++) {
            if (jsmenu['timer'][i]) clearTimeout(jsmenu['timer'][i]);
        }
    }

    initCtrl(ctrlobj, click, duration, timeout, layer);
    ctrlobjclassName = ctrlobj.className;
    ctrlobj.className += ' hover';
    initMenu(ctrlid, menuobj, duration, timeout, layer, drag);

    menuobj.style.display = '';
    if (!is_opera) {
        menuobj.style.clip = 'rect(auto, auto, auto, auto)';
    }

    setMenuPosition(showid, offset);

    if (maxh && menuobj.scrollHeight > maxh) {
        menuobj.style.height = maxh + 'px';
        if (is_opera) {
            menuobj.style.overflow = 'auto';
        } else {
            menuobj.style.overflowY = 'auto';
        }
    }

    if (!duration) {
        setTimeout('hideMenu(' + layer + ')', timeout);
    }

    jsmenu['active'][layer] = menuobj;
}

function setMenuPosition(showid, offset) {
    var showobj = document.getElementById(showid);
    var menuobj = document.getElementById(showid + '_menu');
    if (isUndefined(offset)) offset = 0;
    if (showobj) {
        showobj.pos = fetchOffset(showobj);
        showobj.X = showobj.pos['left'];
        showobj.Y = showobj.pos['top'];
        if (document.getElementById(InFloat) != null) {
            var InFloate = InFloat.split('_');
            if (!floatwinhandle[InFloate[1] + '_1']) {
                floatwinnojspos = fetchOffset(document.getElementById('floatwinnojs'));
                floatwinhandle[InFloate[1] + '_1'] = floatwinnojspos['left'];
                floatwinhandle[InFloate[1] + '_2'] = floatwinnojspos['top'];
            }
            showobj.X = showobj.X - document.getElementById(InFloat).scrollLeft - parseInt(floatwinhandle[InFloate[1] + '_1']);
            showobj.Y = showobj.Y - document.getElementById(InFloat).scrollTop - parseInt(floatwinhandle[InFloate[1] + '_2']);
            InFloat = '';
        }
        showobj.w = showobj.offsetWidth;
        showobj.h = showobj.offsetHeight;
        menuobj.w = menuobj.offsetWidth;
        menuobj.h = menuobj.offsetHeight;
        if (offset < 3) {
            menuobj.style.left = (showobj.X + menuobj.w > document.body.clientWidth) && (showobj.X + showobj.w - menuobj.w >= 0) ? showobj.X + showobj.w - menuobj.w + 'px' : showobj.X + 'px';
            menuobj.style.top = offset == 1 ? showobj.Y + 'px' : (offset == 2 || ((showobj.Y + showobj.h + menuobj.h > document.documentElement.scrollTop + document.documentElement.clientHeight) && (showobj.Y - menuobj.h >= 0)) ? (showobj.Y - menuobj.h) + 'px' : showobj.Y + showobj.h + 'px');
        } else if (offset == 3) {
            menuobj.style.left = (document.body.clientWidth - menuobj.clientWidth) / 2 + document.body.scrollLeft + 'px';
            menuobj.style.top = (document.body.clientHeight - menuobj.clientHeight) / 2 + document.body.scrollTop + 'px';
        }

        if (menuobj.style.clip && !is_opera) {
            menuobj.style.clip = 'rect(auto, auto, auto, auto)';
        }
    }
}

function hideMenu(layer) {
    if (isUndefined(layer)) layer = 0;
    if (jsmenu['active'][layer]) {
        try {
            document.getElementById(jsmenu['active'][layer].ctrlkey).className = ctrlobjclassName;
        } catch (e) { }
        clearTimeout(jsmenu['timer'][jsmenu['active'][layer].ctrlkey]);
        jsmenu['active'][layer].style.display = 'none';
        if (is_ie && is_ie < 7 && jsmenu['iframe'][layer]) {
            jsmenu['iframe'][layer].style.display = 'none';
        }
        jsmenu['active'][layer] = null;
    }
}

function fetchOffset(obj) {
    var left_offset = obj.offsetLeft;
    var top_offset = obj.offsetTop;
    while ((obj = obj.offsetParent) != null) {
        left_offset += obj.offsetLeft;
        top_offset += obj.offsetTop;
    }
    return { 'left': left_offset, 'top': top_offset };
}

function ebygum(eventobj) {
    if (!eventobj || is_ie) {
        window.event.cancelBubble = true;
        return window.event;
    } else {
        if (eventobj.target.type == 'submit') {
            eventobj.target.form.submit();
        }
        eventobj.stopPropagation();
        return eventobj;
    }
}

function img_loaded(myimg, mywidth, myheight) {
    var tmp_img = new Image();
    tmp_img.src = myimg.src;
    image_x = tmp_img.width;
    image_y = tmp_img.height;

    if (image_x > mywidth) {
        tmp_img.height = image_y * mywidth / image_x;
        tmp_img.width = mywidth;

        if (tmp_img.height > myheight) {
            tmp_img.width = tmp_img.width * myheight / tmp_img.height;
            tmp_img.height = myheight;
        }
    }
    else if (image_y > myheight) {
        tmp_img.width = image_x * myheight / image_y;
        tmp_img.height = myheight;

        if (tmp_img.width > mywidth) {
            tmp_img.height = tmp_img.height * mywidth / tmp_img.width;
            tmp_img.width = mywidth;
        }
    }

    myimg.width = tmp_img.width;
    myimg.height = tmp_img.height;
}

////////////////////////////////////////////
///pop操作//
////////////////////////////////////////////

//回显操作
function redisplay(val1, val2, val3) {
    jQuery('#' + val2 + '_value').val(val3);
    jQuery('#' + val2 + '_str').html(val1);
    jQuery('#' + val2 + '_div').slideUp();
}
//查询操作
function datasearch(parmArray) {
    if (event.keyCode == 13) {
        event.keyCode = 9;
    }
    var objArray = {
        controlName: "",
        showDiv: "showdata",
        searchCn: "",
        searchEn: "",
        routestr: "",
        realName: "",
        boatstr: "",
        infid: 0,
        noinfid: 0,
        epinfid:"",
        scid: 0
    };

    parmArray = parmArray || {};
    jQuery.extend(objArray, parmArray);

    var urlstr = 'search_txt_name=' + encodeURIComponent(objArray.searchCn) + '&search_txt_ename=' + encodeURIComponent(objArray.searchEn);
    if (objArray.routestr != "") urlstr += '&routestr=' + objArray.routestr;
    if (objArray.boatstr != "") urlstr += '&boatstr=' + objArray.boatstr;
    if (objArray.realName != "") urlstr += '&search_txt_rname=' + encodeURIComponent(objArray.realName);
    if (objArray.epinfid != "") urlstr += '&epinfid=' + encodeURIComponent(objArray.epinfid);
    if (objArray.scid > 0) urlstr += "&cid=" + objArray.scid;
    if (objArray.infid > 0) urlstr += "&infid=" + objArray.infid;
    if (objArray.noinfid > 0) urlstr += "&noinfid=" + objArray.noinfid;
    AjaxHelper.Updater(objArray.controlName, objArray.showDiv, urlstr);
}